package me.raih.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

/**
 * @Author Raih
 * @Time 2021/1/5 16:00
 *
 *
 * 时间工具
 */
public class TimeUtils {

    /**
     * 创建订单  与时间相结合
     * @return
     */
    public static String createBillNo(){

        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSS");
        String format = LocalDateTime.now().format(df);

        String uuid = UUID.randomUUID().toString().replace("-", "");

        return format + uuid.substring(5, 10);
    }

    /**
     * 时间转换成字符串
     * @param localDateTime 时间戳
     * @return 字符串
     */
    public static String dateFormat(LocalDateTime localDateTime){

        LocalDateTime now = localDateTime.now();

        return now.toString();
    }

    /**
     * 字符串转化成时间
     * @param time
     * @return
     */
    public static Date dateFormat(String time){

        SimpleDateFormat sdf = new SimpleDateFormat();

        try {
            Date date = sdf.parse(time);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return new Date();
    }

    /**
     * 将 date 数据转换成 所需要的时间格式
     * @param date
     * @return
     */
    public static String changeTimeDesc(Date date){
        StringBuffer sb = new StringBuffer();
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        sb.append(instance.get(Calendar.YEAR));
        sb.append("年");
        sb.append(instance.get(Calendar.MONTH)+1);
        sb.append("月");
        return sb.toString();
    }

    public static String billTimeDesc(Date date){
        StringBuffer sb = new StringBuffer();
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        sb.append(instance.get(Calendar.YEAR));
        sb.append("年");
        sb.append(instance.get(Calendar.MONTH)+1);
        sb.append("月");
        return sb.toString();
    }

}
